/*
 * SiFiSy - A File System Simulator
 * Copyright (C) 2010  SevenSoft
 *
 * This file is part of SiFiSy.
 *
 * SiFiSy is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * SiFiSy is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SiFiSy.  If not, see <http://www.gnu.org/licenses/>.
 */

package sifisy.newgui;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Vector;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultTreeModel;
import sifisy.core.*;
import sifisy.connections.*;
import sifisy.connections.SimulationManager.*;

/**
 * SimulationPanel is a panel that represents a SiFiSy Simulation
 * @author Giuseppe Biolo
 * @version %I%, %G%
 * @createData 12/05/2010
 */
public class SimulationPanel extends JPanel {
    private static final String CONFIG_PANEL = "configPanel";
    private static final String RESULTS_PANEL = "resultsPanel";

    private int simulationId;
    private String simulationName;
    private SimulationManager simulationManager;

    private SifisyTreeNode node;
    private ProjectPanel parentPanel;
    private JButton configSimulation;
    private JButton startSimulation;
    private JButton simulationGraphics;
    private JButton exportResults;
    private JButton resetSimulation;
    private JButton setAsDefaultSimulation;
    private JButton closeSimulation;
    private JButton changeFileInsertionMode;
    private JLabel ext2FSType_l;
    private JLabel ext2FSBlockDimension_l;
    private JLabel ext2FSAddressLevel_l;
    private JLabel ext2FSFirstInodeDirectAddresses_l;
    private JLabel ext2FSInodeDimension_l;
    private JLabel ext2FSMainRecord_l;
    private JLabel ext2FSExtensionRecord_l;
    private JLabel partitionDimension_l;
    private JLabel fileDimension_l;
    private JLabel fileContiguousBlocks_l;
    private JComboBox ext2FSType_c;
    private JComboBox ext2FSBlockDimension_c;
    private JComboBox ext2FSAddressLevel_c;
    private JTextField ext2FSBlockDimension_t;
    private JTextField ext2FSFirstInodeDirectAddresses_t;
    private JTextField ext2FSInodeDimension_t;
    private JLabel hdDimension_l;
    private JTextField hdDimension_t;
    private JComboBox hdDimension_c;

    private JLabel hdFileBlocksOnTrack_l;
    private JTextField hdFileBlocksOnTrack_t;

    private JLabel hdSeekTime_l;
    private JTextField hdSeekTime_t;
    private JLabel hdBlocksPerTrack_l;
    private JTextField hdBlocksPerTrack_t;
    private JLabel hdRotationSpeed_l;
    private JTextField hdRotationSpeed_t;
    private Checkbox fmAddSingleFile;
    private Checkbox fmAddMoreFiles;
    private JLabel fmFileDimension_l;
    private JTextField fmFileDimension_t;
    private JComboBox fmFileDimension_c;
    private JLabel fmFragmentation_l;
    private JTextField fmFragmentation_t;
    private JTextArea messagesText;
    private JPanel multipleFileManager;
    private JPanel singleFileManager;
    private JPanel simManager;
    private JButton multipleFileInsertionMode_b;
    private JButton singleFileInsertionMode_b;
    private JLabel singleFileDimension_l;
    private JTextField singleFileDimension_t;
    private JComboBox singleFileDimension_c;
    private JLabel singleFileContiguousBlocks_l;
    private JTextField singleFileContiguousBlocks_t;
    private JTextField singleFileMaxDimension_t;
    private JLabel singleFileMaxExponent_l;
    private JLabel multipleFileName_l;
    private JTextField multipleFileName_t;
    private JLabel multipleFileSize_l;
    private JTextField multipleFileSize_t;
    private JComboBox multipleFileSize_c;
    private JButton multipleInsertFile_b;
    private JTextField multipleAvaibleDiskSpace_t;
    private JLabel multipleAvaibleDiskSpace_l;
    private JComboBox multipleInsertionPolicy_c;
    private JButton multipleRemoveFile_b;
    private JTable multipleFileTable;
    private JScrollPane multipleFileTableScrollPane;
    private DefaultTableModel model;
    //private JScrollPane configurationPanelScrollPane;
    //private JList multipleFileList;

    private JPanel ext2FSConfiguration;
    private JPanel ntfsFSConfiguration;
    private JPanel fat32FSConfiguration;

    private JLabel ntfsFSType_l;
    private JComboBox ntfsFSType_c;
    private JLabel ntfsFSBlockDimension_l;
    private JTextField ntfsFSBlockDimension_t;
    private JComboBox ntfsFSBlockDimension_c;
    private JLabel ntfsFSMainRecord_l;
    private JTextField ntfsFSMainRecord_t;
    private JLabel ntfsFSExtensionRecord_l;
    private JTextField ntfsFSExtensionRecord_t;

    private JLabel fat32FSType_l;
    private JComboBox fat32FSType_c;
    private JLabel fat32FSBlockDimension_l;
    private JTextField fat32FSBlockDimension_t;
    private JComboBox fat32FSBlockDimension_c;

    private JPanel innerPanel;
    private SimulationResultsPanel resultsPanel;
    private JFrame frameGraphics;

  //  private String activePanel;

    // Bottone avanzate per configurare le avanzate del disco
    private JButton hdAdvancedButton;

    // Pannello contenente le configurazioni avanzate del disco
    JPanel hdAdvancedConfiguration;

    /**
     * Initialize class fields and settings
     */
    private void initComponents() {
        configSimulation = new JButton(Model.translate("config"));
        //configSimulation.setIcon(new ImageIcon(getClass().getResource("icons/small/configSimulation.png")));
        startSimulation = new JButton(Model.translate("startSimulation"));
        startSimulation.setIcon(new ImageIcon(getClass().getResource("icons/small/startSimulation.png")));
        simulationGraphics = new JButton(Model.translate("showResult"));
        //simulationGraphics.setIcon(new ImageIcon(getClass().getResource("icons/small/simulationGraphics.png")));
        exportResults = new JButton(Model.translate("export"));
        resetSimulation = new JButton(Model.translate("resetSimulation"));
        setAsDefaultSimulation = new JButton(Model.translate("setAsDefaultSimulation"));
        closeSimulation = new JButton(Model.translate("closeSimulation"));
        closeSimulation.setIcon(new ImageIcon(getClass().getResource("icons/small/close.png")));
        ext2FSType_l = new JLabel(Model.translate("type")+":");
        ext2FSBlockDimension_l = new JLabel(Model.translate("blockDimension")+":");
        ext2FSAddressLevel_l = new JLabel(Model.translate("addressLevel")+":");
        ext2FSFirstInodeDirectAddresses_l = new JLabel(Model.translate("block1stInode")+":");
        ext2FSInodeDimension_l = new JLabel(Model.translate("inodeDimension")+":");
        ext2FSMainRecord_l = new JLabel(Model.translate("forMainRecord")+":");
        ext2FSExtensionRecord_l = new JLabel(Model.translate("forExtensionRecord")+":");
        partitionDimension_l = new JLabel(Model.translate("dimension")+":");
        fileDimension_l = new JLabel(Model.translate("dimension")+":");
        fileContiguousBlocks_l = new JLabel(Model.translate("fragmentationNumber")+":");
        ext2FSType_c = new JComboBox();
        ext2FSType_c.addItem("EXT2");
        ext2FSType_c.addItem("NTFS");
        ext2FSType_c.addItem("FAT32");
        ext2FSBlockDimension_c = new JComboBox();
        ext2FSBlockDimension_c.addItem("KB");
        ext2FSBlockDimension_c.addItem("MB");
        //ext2FSBlockDimension_c.addItem("GB");
        ext2FSAddressLevel_c = new JComboBox();
        ext2FSAddressLevel_c.addItem("1");
        ext2FSAddressLevel_c.addItem("2");
        ext2FSAddressLevel_c.addItem("3");
        ext2FSBlockDimension_t = new JTextField(5);
        ext2FSFirstInodeDirectAddresses_t = new JTextField(5);
        ext2FSInodeDimension_t = new JTextField(5);
        hdDimension_l = new JLabel(Model.translate("dimension")+":");
        hdDimension_t = new JTextField(5);
        hdDimension_c = new JComboBox();
        //hdDimension_c.addItem("KB");
        hdDimension_c.addItem("MB");
        hdDimension_c.addItem("GB");
        hdDimension_c.addItem("TB");
        //hdFileBlocksOnTrack_l = new JLabel(Model.translate("blockFileTrack")+":");
        //hdFileBlocksOnTrack_t = new JTextField(5);
        hdSeekTime_l = new JLabel(Model.translate("seekTime")+":");
        hdSeekTime_t = new JTextField(5);
        hdBlocksPerTrack_l = new JLabel(Model.translate("trackDimension")+":");
        hdBlocksPerTrack_t = new JTextField(5);
        hdRotationSpeed_l = new JLabel(Model.translate("rotationSpeed")+":");
        hdRotationSpeed_t = new JTextField(5);




        hdAdvancedButton = new JButton(Model.translate("advanced"));
        hdAdvancedConfiguration = new JPanel(new GridLayout(4,2));
        hdAdvancedConfiguration.setVisible(false);



        fmAddSingleFile = new Checkbox(Model.translate("fragmentation"), null, true);
        fmAddMoreFiles = new Checkbox(Model.translate("management"), false);
        fmFileDimension_l = new JLabel(Model.translate("dimension")+":");
        fmFileDimension_t = new JTextField(5);
        fmFileDimension_c = new JComboBox();
        fmFileDimension_c.addItem("KB");
        fmFileDimension_c.addItem("MB");
        fmFileDimension_c.addItem("GB");
        fmFragmentation_l = new JLabel(Model.translate("fragmentationNumber")+":");
        fmFragmentation_t = new JTextField(5);
        messagesText = new JTextArea(Model.translate("messages"));
        messagesText.setEditable(false);
        changeFileInsertionMode = new JButton();
        multipleFileManager = new JPanel();
        singleFileManager = new JPanel(new BorderLayout());
        simManager = new JPanel(new FlowLayout());
        multipleFileInsertionMode_b = new JButton(Model.translate("singleFile"));
        singleFileInsertionMode_b = new JButton(Model.translate("moreFile"));
        singleFileDimension_l = new JLabel(Model.translate("fileDimension")+":");
        singleFileDimension_t = new JTextField(5);
        singleFileDimension_c = new JComboBox();
        singleFileDimension_c.addItem("KB");
        singleFileDimension_c.addItem("MB");
        singleFileDimension_c.addItem("GB");
        singleFileContiguousBlocks_l = new JLabel(Model.translate("contiguousBlocks")+":");
        singleFileContiguousBlocks_t = new JTextField(5);


        singleFileMaxDimension_t = new JTextField(5);
        singleFileMaxDimension_t.setEditable(false);
        singleFileMaxDimension_t.setText("--");
        singleFileMaxExponent_l = new JLabel("MB");



        multipleFileName_l = new JLabel(Model.translate("name")+":");
        multipleFileName_t = new JTextField(5);
        multipleFileSize_l = new JLabel(Model.translate("dimension")+":");
        multipleFileSize_t = new JTextField(5);
        multipleFileSize_c = new JComboBox();
        multipleFileSize_c.addItem("KB");
        multipleFileSize_c.addItem("MB");
        multipleFileSize_c.addItem("GB");
        multipleInsertFile_b = new JButton(Model.translate("insert"));
        multipleAvaibleDiskSpace_t = new JTextField("--");
        multipleAvaibleDiskSpace_t.setEditable(false);
        multipleAvaibleDiskSpace_l = new JLabel("KB");
        multipleInsertionPolicy_c = new JComboBox();
        multipleInsertionPolicy_c.addItem("First-Fit");
        multipleInsertionPolicy_c.addItem("Best-Fit");
        multipleInsertionPolicy_c.addItem("Next-Fit");
        multipleInsertionPolicy_c.addItem("Worst-Fit");
        multipleRemoveFile_b = new JButton(Model.translate("remuveFile"));
        //multipleRemoveFile_b.setEnabled(false);
        model = new DefaultTableModel(new String[] {Model.translate("name"),Model.translate("dimension")},0);

        multipleFileTable = new JTable(model) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        multipleFileTableScrollPane = new JScrollPane(multipleFileTable);

        ntfsFSType_l = new JLabel(Model.translate("type")+":");
        ntfsFSType_c = new JComboBox();
        ntfsFSType_c.addItem("EXT2");
        ntfsFSType_c.addItem("NTFS");
        ntfsFSType_c.addItem("FAT32");
        ntfsFSBlockDimension_l = new JLabel(Model.translate("blockDimension")+":");
        ntfsFSBlockDimension_t = new JTextField(5);
        ntfsFSBlockDimension_c = new JComboBox();
        ntfsFSBlockDimension_c.addItem("KB");
        ntfsFSBlockDimension_c.addItem("MB");
        //ntfsFSBlockDimension_c.addItem("GB");
        ntfsFSMainRecord_l = new JLabel(Model.translate("mainRecord")+":");
        ntfsFSMainRecord_t = new JTextField(5);
        ntfsFSExtensionRecord_l = new JLabel(Model.translate("extensionRecord")+":");
        ntfsFSExtensionRecord_t = new JTextField(5);

        fat32FSType_l = new JLabel(Model.translate("type")+":");
        fat32FSType_c = new JComboBox();
        fat32FSType_c.addItem("EXT2");
        fat32FSType_c.addItem("NTFS");
        fat32FSType_c.addItem("FAT32");
        fat32FSBlockDimension_l = new JLabel(Model.translate("blockDimension")+":");
        fat32FSBlockDimension_t = new JTextField(5);
        fat32FSBlockDimension_c = new JComboBox();
        fat32FSBlockDimension_c.addItem("KB");
        fat32FSBlockDimension_c.addItem("MB");
        //fat32FSBlockDimension_c.addItem("GB");

        // Adding of listeners
        ext2FSType_c.addItemListener(simulationManager.new TestComboFSType());
        hdDimension_t.addKeyListener(simulationManager.new TestDimPart());
        hdDimension_c.addItemListener(simulationManager.new TestComboDimPart());
        ext2FSBlockDimension_t.addKeyListener(simulationManager.new TestDimBlock());
        ext2FSBlockDimension_c.addItemListener(simulationManager.new TestComboDimBlock());
        ext2FSInodeDimension_t.addKeyListener(simulationManager.new TestDimiNode());
        ext2FSFirstInodeDirectAddresses_t.addKeyListener(simulationManager.new TestDirectBlockFirstiNode());
        //AGGIUNTO LISTENER
        ext2FSAddressLevel_c.addItemListener(simulationManager.new TestComboMaxAddressLevel());

        ntfsFSBlockDimension_t.addKeyListener(simulationManager.new TestDimBlock());
        ntfsFSBlockDimension_c.addItemListener(simulationManager.new TestComboDimBlock());
        ntfsFSMainRecord_t.addKeyListener(simulationManager.new TestMainRecord());
        ntfsFSExtensionRecord_t.addKeyListener(simulationManager.new TestExtensionRecord());
        fat32FSBlockDimension_t.addKeyListener(simulationManager.new TestDimBlock());
        fat32FSBlockDimension_c.addItemListener(simulationManager.new TestComboDimBlock());
        hdSeekTime_t.addKeyListener(simulationManager.new TestSeekTime());
        hdBlocksPerTrack_t.addKeyListener(simulationManager.new TestTrack());
        hdRotationSpeed_t.addKeyListener(simulationManager.new TestRotationSpeed());
        //AGGIUNTO LISTENER
        //hdFileBlocksOnTrack_t.addKeyListener(simulationManager.new TestBlockFileTrack());

        fmFileDimension_t.addKeyListener(simulationManager.new TestDimFile());
        fmFileDimension_c.addItemListener(simulationManager.new TestComboDimFile());
        fmFragmentation_t.addKeyListener(simulationManager.new TestFragmentation());

        // FILE MULTIPLI!!!
        singleFileDimension_t.addKeyListener(simulationManager.new TestDimFile());
        singleFileDimension_c.addItemListener(simulationManager.new TestComboDimFile());
        singleFileContiguousBlocks_t.addKeyListener(simulationManager.new TestFragmentation());
        
        multipleFileName_t.addKeyListener(simulationManager.new TestFileName());
        multipleFileSize_t.addKeyListener(simulationManager.new TestDimMultiFile());
        multipleFileSize_c.addItemListener(simulationManager.new TestComboDimMultiFile());
        multipleAvaibleDiskSpace_t.addKeyListener(simulationManager.new TestTrack());
        multipleInsertionPolicy_c.addItemListener(simulationManager.new TestComboAllocationType());



        hdAdvancedButton.addMouseListener(simulationManager.new ShowHideAdvancedPanel());

        singleFileInsertionMode_b.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                singleFileManager.setVisible(false);
                multipleFileManager.setVisible(true);
                simulationManager.updateError();
            }
        });

        multipleFileInsertionMode_b.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                singleFileManager.setVisible(true);
                multipleFileManager.setVisible(false);
                simulationManager.updateError();
            }
        });

        multipleInsertFile_b.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(multipleInsertFile_b.isEnabled()){
                model.addRow(new Object[]{multipleFileName_t.getText(),multipleFileSize_t.getText() + " " + multipleFileSize_c.getSelectedItem().toString()});
                simulationManager.testAddFile();
                }
            }
        });

        multipleRemoveFile_b.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int index = multipleFileTable.getSelectedRow();
                if (index >= 0) {
                    simulationManager.testDeleteFile(multipleFileTable.getValueAt(index, 0).toString(), multipleFileTable.getValueAt(index, 1).toString());
                    model.removeRow(multipleFileTable.getSelectedRow());
                }
            }
        });

        /*NUOVO CONTROLLER PER LA VISUALIZZAZIONE DEI PANNELLI!!*/
        ext2FSType_c.addActionListener(simulationManager.new SwitchFileSytemPanel());
        ntfsFSType_c.addActionListener(simulationManager.new SwitchFileSytemPanel());
        fat32FSType_c.addActionListener(simulationManager.new SwitchFileSytemPanel());
        
        //BOTTONI
        configSimulation.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showConfig();
            }
        });

        startSimulation.addActionListener(simulationManager.new StartSimulation());
  
        resetSimulation.addActionListener(simulationManager.new ResetToDefault());

        setAsDefaultSimulation.addActionListener(simulationManager.new TestSetAsDefault());

        closeSimulation.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Model.removeSimulation(simulationId);
                ((DefaultTreeModel)MainWidget.getTreeView().getModel()).removeNodeFromParent(SimulationPanel.this.node);
                MainWidget.removeSimulationFromList(SimulationPanel.this.getSimulationName());
                if (parentPanel != null) {
                    parentPanel.removeSimulation(SimulationPanel.this);
                }
                else {
                    MainWidget.getTabbedPane().remove(SimulationPanel.this);
                }
            }
        });
        simulationGraphics.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               /*
                try {
                    Model.exportSimulationPdf(resultsPanel, "");
                } catch (DocumentException ex) {
                    Logger.getLogger(SimulationPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
                */
                int simId = SimulationPanel.this.getSimId();

                frameGraphics = new JFrame(Model.translate("simulationGraphic"));
                frameGraphics.setLayout(new BorderLayout());
                frameGraphics.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

                JToolBar toolbar = new JToolBar();
                toolbar.setFloatable(false);
                JButton exportPng = new JButton(Model.translate("exportImage"));
                exportPng.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        String filePath = MainWidget.showSaveFileDialog(MainWidget.FILEFILTER_PNG);
                        if (!filePath.equals("")) {
                            SimulationPanel.this.simulationManager.exportGraphicsPng(filePath);
                        }
                    }
                });
                toolbar.add(exportPng);

                frameGraphics.add(toolbar, BorderLayout.NORTH);

                if (Model.getFsType(simId).equals("EXT2")) {
                    InodeSchema ext2 = new InodeSchema(simId);
                    frameGraphics.add(ext2, BorderLayout.CENTER);
                }
                if (Model.getFsType(simId).equals("FAT32")) {
                    FatSchema fat = new FatSchema(simId);
                    frameGraphics.add(fat, BorderLayout.CENTER);
                }
                if (Model.getFsType(simId).equals("NTFS")) {
                    NtfsSchema ntfs = new NtfsSchema(simId);
                    frameGraphics.add(ntfs, BorderLayout.CENTER);
                }

                frameGraphics.setSize(700, 650);
                frameGraphics.setLocationRelativeTo(MainWidget.getMainWidgetFrame());
                frameGraphics.setVisible(true);
            }
        });
        exportResults.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String filePath = MainWidget.showSaveFileDialog(MainWidget.FILEFILTER_PDF);

                if (!filePath.equals("")) {
                    simulationManager.exportPdf(filePath);
                }
            }
        });
    }

    /**
     * Return the hdAdvancedButton instance
     * @return hdAdvancedButton
     */
    public JButton getHdAdvancedButton() {
        return hdAdvancedButton;
    }

    /**
     * Return the hdAdvancedConfiguration instance
     * @return hdAdvancedConfiguration
     */
    public JPanel getHdAdvancedConfiguration() {
        return hdAdvancedConfiguration;
    }

    /**
     * Return the ext2FSConfiguration instance
     * @return ext2FSConfiguration
     */
    public JPanel getExt2FSConfiguration() {
        return ext2FSConfiguration;
    }

    /**
     * Return the fat32FSConfiguration instance
     * @return fat32FSConfiguration
     */
    public JPanel getFat32FSConfiguration() {
        return fat32FSConfiguration;
    }

    /**
     * Return the ntfsFSConfiguration instance
     * @return ntfsFSConfiguration
     */
    public JPanel getNtfsFSConfiguration() {
        return ntfsFSConfiguration;
    }

    /**
     * Return the ext2FSType_c instance
     * @return ext2FSType_c
     */
    public JComboBox getExt2FSType_c() {
        return ext2FSType_c;
    }

    /**
     * Return the fat32FSType_c instance
     * @return fat32FSType_c
     */
    public JComboBox getFat32FSType_c() {
        return fat32FSType_c;
    }

    /**
     * Return the ntfsFSType_c instance
     * @return ntfsFSType_c
     */
    public JComboBox getNtfsFSType_c() {
        return ntfsFSType_c;
    }

    /**
     * Sets the foreground color of the components to the default state
     */
    public void clearFields() {
        hdDimension_t.setForeground(Color.black);
        //hdDimension_c;
        // ?? hdFileBlocksOnTrack_t.setText((new Integer(Model.getTrackSectors(simulationId))).toString());
        hdSeekTime_t.setForeground(Color.black);
        //hdBlocksPerTrack_t.setForeground(Color.black);
        hdRotationSpeed_t.setForeground(Color.black);
        ext2FSBlockDimension_t.setForeground(Color.black);
        ext2FSFirstInodeDirectAddresses_t.setForeground(Color.black);
        ext2FSInodeDimension_t.setForeground(Color.black);
        ntfsFSBlockDimension_t.setForeground(Color.black);
        ntfsFSMainRecord_t.setForeground(Color.black);
        ntfsFSExtensionRecord_t.setForeground(Color.black);
        fat32FSBlockDimension_t.setForeground(Color.black);
        singleFileDimension_t.setForeground(Color.black);
        singleFileContiguousBlocks_t.setForeground(Color.black);
        messagesText.setText("");
    }

    /**
     * Read the values of the current simulation from the Model
     * and load them into the components
     */
    public void readSimulationValues() {
        hdDimension_t.setText((new Integer(Model.getPartitionDimension(simulationId))).toString());

        switch(Model.getPartitionExponent(simulationId)) {
//            case 10: hdDimension_c.setSelectedIndex(0);
//                        break;
            case 20: hdDimension_c.setSelectedIndex(0);
                        break;
            case 30: hdDimension_c.setSelectedIndex(1);
                        break;
            case 40: hdDimension_c.setSelectedIndex(2);
                        break;
        }

        //hdFileBlocksOnTrack_t.setText((new Integer(Model.getTrackSectors(simulationId))).toString());
        hdSeekTime_t.setText((new Integer(Model.getSeekTime(simulationId))).toString());
        hdBlocksPerTrack_t.setText((new Integer(Model.getTrackPercent(simulationId))).toString());
        hdRotationSpeed_t.setText((new Integer(Model.getDiskRotationSpeed(simulationId))).toString());
        if((Model.getFsType(simulationId)).equals("EXT2")) {
            ext2FSConfiguration.setVisible(true);
            ntfsFSConfiguration.setVisible(false);
            fat32FSConfiguration.setVisible(false);
            ext2FSType_c.setSelectedItem(new String("EXT2"));
            switch(Model.getBlockExponent(simulationId)) {
                case 10: ext2FSBlockDimension_c.setSelectedItem(new String("KB"));
                            break;
                case 20: ext2FSBlockDimension_c.setSelectedItem(new String("MB"));
                            break;
//                case 30: ext2FSBlockDimension_c.setSelectedItem(new String("GB"));
//                            break;
            }
            ext2FSAddressLevel_c.setSelectedItem(Integer.toString(Model.getAddressLevel(simulationId)));
            ext2FSBlockDimension_t.setText(Integer.toString(Model.getBlockDimension(simulationId)));
            ext2FSFirstInodeDirectAddresses_t.setText(Integer.toString(Model.getBlockFirstInode(simulationId)));
            ext2FSInodeDimension_t.setText(Integer.toString(Model.getInodeDimension(simulationId)));
        }
        if((Model.getFsType(simulationId)).equals("NTFS")) {
            ext2FSConfiguration.setVisible(false);
            ntfsFSConfiguration.setVisible(true);
            fat32FSConfiguration.setVisible(false);
            ntfsFSType_c.setSelectedItem(new String("NTFS"));
            switch(Model.getBlockExponent(simulationId)) {
                case 10: ntfsFSBlockDimension_c.setSelectedItem(new String("KB"));
                            break;
                case 20: ntfsFSBlockDimension_c.setSelectedItem(new String("MB"));
                            break;
                case 30: ntfsFSBlockDimension_c.setSelectedItem(new String("GB"));
                            break;
            }
            ntfsFSBlockDimension_t.setText(Integer.toString(Model.getBlockDimension(simulationId)));
            ntfsFSMainRecord_t.setText(Integer.toString(Model.getMainRecord(simulationId)));
            ntfsFSExtensionRecord_t.setText(Integer.toString(Model.getExtensionRecord(simulationId)));
        }
        if((Model.getFsType(simulationId)).equals("FAT32")) {
            ext2FSConfiguration.setVisible(false);
            ntfsFSConfiguration.setVisible(false);
            fat32FSConfiguration.setVisible(true);
            fat32FSType_c.setSelectedItem(new String("FAT32"));
            switch(Model.getBlockExponent(simulationId)) {
                case 10: fat32FSBlockDimension_c.setSelectedItem(new String("KB"));
                            break;
                case 20: fat32FSBlockDimension_c.setSelectedItem(new String("MB"));
                            break;
                case 30: fat32FSBlockDimension_c.setSelectedItem(new String("GB"));
                            break;
            }
            fat32FSBlockDimension_t.setText(Integer.toString(Model.getBlockDimension(simulationId)));
        }
        singleFileDimension_t.setText(Integer.toString(Model.getSimulation(simulationId).getFiles().elementAt(0).getDimension()));
        switch(Model.getSimulation(simulationId).getFiles().elementAt(0).getExponent()) {
            case 10: singleFileDimension_c.setSelectedItem(new String("KB"));
                    break;
            case 20: singleFileDimension_c.setSelectedItem(new String("MB"));
                    break;
            case 30: singleFileDimension_c.setSelectedItem(new String("GB"));
                    break;
        }
        singleFileContiguousBlocks_t.setText(String.valueOf(Model.getSimulation(simulationId).getFiles().elementAt(0).getAverageBlocks()));
    }

    /**
     * SimulationPanel default constructor
     */
    public SimulationPanel() { }

    /**
     * Create a SimulationPanel
     * @param simName the name of the simulation
     * @param node the node in the tree representing this panel
     */
    public SimulationPanel(String simName, SifisyTreeNode node) {
        this(simName, node, null);
    }

    /**
     * Create a SimulationPanel as a child of a ProjectPanel
     * @param simName: the name of the simulation
     * @param node: the node in the tree representing this panel
     * @param parent: the parent ProjectPanel
     */
    public SimulationPanel(String simName, SifisyTreeNode node, ProjectPanel parent) {
        simulationName = simName;
        simulationManager = new SimulationManager(this,simulationName);
        this.node = node;
        parentPanel = parent;
        initComponents();
        //readSimulationValues();
        JToolBar toolBar = new JToolBar();
        toolBar.setFloatable(false);
        toolBar.add(configSimulation);
        toolBar.add(startSimulation);
        toolBar.add(simulationGraphics);
        toolBar.add(exportResults);
        toolBar.add(resetSimulation);
        //toolBar.add(setAsDefaultSimulation);
        toolBar.add(closeSimulation);
        
        JPanel ext2FSTypeLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ext2FSTypeLabel.add(ext2FSType_l);

        JPanel ext2FSTypeCombo = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ext2FSTypeCombo.add(ext2FSType_c);

        JPanel ext2FSBlockDimensionLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ext2FSBlockDimensionLabel.add(ext2FSBlockDimension_l);

        JPanel ext2FSBlockDimension = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ext2FSBlockDimension.add(ext2FSBlockDimension_t);
        ext2FSBlockDimension.add(ext2FSBlockDimension_c);

        JPanel ext2FSAddressLevelLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ext2FSAddressLevelLabel.add(ext2FSAddressLevel_l);

        JPanel ext2FSAddressLevelCombo = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ext2FSAddressLevelCombo.add(ext2FSAddressLevel_c);

        JPanel ext2FSFirstInodeDirectAddressesLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ext2FSFirstInodeDirectAddressesLabel.add(ext2FSFirstInodeDirectAddresses_l);

        JPanel ext2FSFirstInodeDirectAddressesText = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ext2FSFirstInodeDirectAddressesText.add(ext2FSFirstInodeDirectAddresses_t);

        JPanel ext2FSInodeDimensionLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ext2FSInodeDimensionLabel.add(ext2FSInodeDimension_l);

        JPanel ext2FSInodeDimension = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ext2FSInodeDimension.add(ext2FSInodeDimension_t);
        ext2FSInodeDimension.add(new JLabel("Byte"));

        ext2FSConfiguration = new JPanel(new GridLayout(5, 2));
        ext2FSConfiguration.setBorder(new TitledBorder("File System"));
        ext2FSConfiguration.add(ext2FSTypeLabel);
        ext2FSConfiguration.add(ext2FSTypeCombo);
        ext2FSConfiguration.add(ext2FSBlockDimensionLabel);
        ext2FSConfiguration.add(ext2FSBlockDimension);
        ext2FSConfiguration.add(ext2FSAddressLevelLabel);
        ext2FSConfiguration.add(ext2FSAddressLevelCombo);
        ext2FSConfiguration.add(ext2FSFirstInodeDirectAddressesLabel);
        ext2FSConfiguration.add(ext2FSFirstInodeDirectAddressesText);
        ext2FSConfiguration.add(ext2FSInodeDimensionLabel);
        ext2FSConfiguration.add(ext2FSInodeDimension);



//////////////////////////////////////////////////

        
 



        JPanel ntfsFSTypeLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ntfsFSTypeLabel.add(ntfsFSType_l);
        
        JPanel ntfsFSTypeCombo = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ntfsFSTypeCombo.add(ntfsFSType_c);
        
        JPanel ntfsFSBlockDimensionLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ntfsFSBlockDimensionLabel.add(ntfsFSBlockDimension_l);
        
        JPanel ntfsFSBlockDimensionValue = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ntfsFSBlockDimensionValue.add(ntfsFSBlockDimension_t);
        ntfsFSBlockDimensionValue.add(ntfsFSBlockDimension_c);
        
        JPanel ntfsFSMainRecordLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ntfsFSMainRecordLabel.add(ntfsFSMainRecord_l);
        
        JPanel ntfsFSMainRecordValue = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ntfsFSMainRecordValue.add(ntfsFSMainRecord_t);
        ntfsFSMainRecordValue.add(new JLabel("Byte"));
        
        JPanel ntfsFSExtensionRecordLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ntfsFSExtensionRecordLabel.add(ntfsFSExtensionRecord_l);
        
        JPanel ntfsFSExtensionRecordValue = new JPanel(new FlowLayout(FlowLayout.LEFT));
        ntfsFSExtensionRecordValue.add(ntfsFSExtensionRecord_t);
        ntfsFSExtensionRecordValue.add(new JLabel("Byte"));
        
        ntfsFSConfiguration = new JPanel(new GridLayout(4,2));
        ntfsFSConfiguration.setBorder(new TitledBorder("File System"));
        ntfsFSConfiguration.add(ntfsFSTypeLabel);
        ntfsFSConfiguration.add(ntfsFSTypeCombo);
        ntfsFSConfiguration.add(ntfsFSBlockDimensionLabel);
        ntfsFSConfiguration.add(ntfsFSBlockDimensionValue);
        ntfsFSConfiguration.add(ntfsFSMainRecordLabel);
        ntfsFSConfiguration.add(ntfsFSMainRecordValue);
        ntfsFSConfiguration.add(ntfsFSExtensionRecordLabel);
        ntfsFSConfiguration.add(ntfsFSExtensionRecordValue);

        

////////////////////////////////////////////////


   


        JPanel fat32FSTypeLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        fat32FSTypeLabel.add(fat32FSType_l);

        JPanel fat32FSTypeCombo = new JPanel(new FlowLayout(FlowLayout.LEFT));
        fat32FSTypeCombo.add(fat32FSType_c);

        JPanel fat32FSBlockDimensionLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        fat32FSBlockDimensionLabel.add(fat32FSBlockDimension_l);

        JPanel fat32FSBlockDimensionValue = new JPanel(new FlowLayout(FlowLayout.LEFT));
        fat32FSBlockDimensionValue.add(fat32FSBlockDimension_t);
        fat32FSBlockDimensionValue.add(fat32FSBlockDimension_c);


        fat32FSConfiguration = new JPanel(new GridLayout(2,2));
        fat32FSConfiguration.setBorder(new TitledBorder("File System"));
        fat32FSConfiguration.add(fat32FSTypeLabel);
        fat32FSConfiguration.add(fat32FSTypeCombo);
        fat32FSConfiguration.add(fat32FSBlockDimensionLabel);
        fat32FSConfiguration.add(fat32FSBlockDimensionValue);


////////////////////////////////////


        JPanel hdDimensionLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        hdDimensionLabel.add(hdDimension_l);

        JPanel hdDimension = new JPanel(new FlowLayout(FlowLayout.LEFT));
        hdDimension.add(hdDimension_t);
        hdDimension.add(hdDimension_c);

        //JPanel hdFileBlocksOnTrackLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        //hdFileBlocksOnTrackLabel.add(hdFileBlocksOnTrack_l);

        //JPanel hdFileBlocksOnTrackText = new JPanel(new FlowLayout(FlowLayout.LEFT));
        //hdFileBlocksOnTrackText.add(hdFileBlocksOnTrack_t);

        JPanel hdSeekTimeLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        hdSeekTimeLabel.add(hdSeekTime_l);

        JPanel hdSeekTime = new JPanel(new FlowLayout(FlowLayout.LEFT));
        hdSeekTime.add(hdSeekTime_t);
        hdSeekTime.add(new JLabel("ms"));

        JPanel hdBlocksPerTrackLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        hdBlocksPerTrackLabel.add(hdBlocksPerTrack_l);

        JPanel hdBlocksPerTrackText = new JPanel(new FlowLayout(FlowLayout.LEFT));
        hdBlocksPerTrackText.add(hdBlocksPerTrack_t);
        hdBlocksPerTrackText.add(new JLabel(Model.translate("blocks")));

        JPanel hdRotationSpeedLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        hdRotationSpeedLabel.add(hdRotationSpeed_l);

        JPanel hdRotationSpeed = new JPanel(new FlowLayout(FlowLayout.LEFT));
        hdRotationSpeed.add(hdRotationSpeed_t);
        hdRotationSpeed.add(new JLabel("RPM"));

        JPanel hdAdvancedButtonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        hdAdvancedButtonPanel.add(hdAdvancedButton);

        JPanel hdDefaultConfiguration = new JPanel(new GridLayout(1,2));
        hdDefaultConfiguration.add(hdDimensionLabel);
        hdDefaultConfiguration.add(hdDimension);

        //hdAdvancedConfiguration.add(hdFileBlocksOnTrackLabel);
        //hdAdvancedConfiguration.add(hdFileBlocksOnTrackText);
        hdAdvancedConfiguration.add(hdSeekTimeLabel);
        hdAdvancedConfiguration.add(hdSeekTime);
        hdAdvancedConfiguration.add(hdBlocksPerTrackLabel);
        hdAdvancedConfiguration.add(hdBlocksPerTrackText);
        hdAdvancedConfiguration.add(hdRotationSpeedLabel);
        hdAdvancedConfiguration.add(hdRotationSpeed);

        JPanel hdConfiguration = new JPanel(new BorderLayout());
        hdConfiguration.setBorder(new TitledBorder(Model.translate("hardDisk")));
        hdConfiguration.add(hdDefaultConfiguration,BorderLayout.NORTH);
        hdConfiguration.add(hdAdvancedConfiguration,BorderLayout.CENTER);
        hdConfiguration.add(hdAdvancedButtonPanel,BorderLayout.SOUTH);

        JPanel hdDiskConfiguration = new JPanel();
        hdDiskConfiguration.setLayout(new BoxLayout(hdDiskConfiguration, BoxLayout.Y_AXIS));

/////////////////

        hdDiskConfiguration.add(ext2FSConfiguration);
        hdDiskConfiguration.add(ntfsFSConfiguration);
        hdDiskConfiguration.add(fat32FSConfiguration);
        hdDiskConfiguration.add(hdConfiguration);

//////////////////////////

        singleFileManager.setBorder(new TitledBorder(Model.translate("addFile")));
        JPanel singleFileInsertionModeButton = new JPanel(new FlowLayout(FlowLayout.CENTER));
        singleFileInsertionModeButton.add(singleFileInsertionMode_b);

        JPanel singleFileDimensionLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        singleFileDimensionLabel.add(singleFileDimension_l);
        JPanel singleFileDimensionGlobal = new JPanel(new GridLayout(2,2));
        JPanel singleFileDimensionValue = new JPanel(new FlowLayout(FlowLayout.LEFT));
        singleFileDimensionValue.add(singleFileDimension_t);
        singleFileDimensionValue.add(singleFileDimension_c);
        JPanel singleFileContiguousBlocksLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        singleFileContiguousBlocksLabel.add(singleFileContiguousBlocks_l);
        JPanel singleFileContiguousBlocksText = new JPanel(new FlowLayout(FlowLayout.LEFT));
        singleFileContiguousBlocksText.add(singleFileContiguousBlocks_t);
        singleFileDimensionGlobal.add(singleFileDimensionLabel);
        singleFileDimensionGlobal.add(singleFileDimensionValue);
        singleFileDimensionGlobal.add(singleFileContiguousBlocksLabel);
        singleFileDimensionGlobal.add(singleFileContiguousBlocksText);

        JPanel singleFileMaxDimensionPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        singleFileMaxDimensionPanel.add(singleFileMaxDimension_t);

        JPanel singleFileMaxExponentPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        singleFileMaxExponentPanel.add(singleFileMaxExponent_l);

        JPanel singleFileMaxDimension = new JPanel(new FlowLayout());
        singleFileMaxDimension.setBorder(new TitledBorder(Model.translate("maxLenght")));
        singleFileMaxDimension.add(singleFileMaxDimensionPanel);
        singleFileMaxDimension.add(singleFileMaxExponentPanel);


        singleFileManager.add(singleFileInsertionModeButton,BorderLayout.NORTH);
        singleFileManager.add(singleFileDimensionGlobal,BorderLayout.CENTER);
        singleFileManager.add(singleFileMaxDimension,BorderLayout.SOUTH);

        JPanel multipleFileInsertionModeButton = new JPanel(new FlowLayout());
        multipleFileInsertionModeButton.add(multipleFileInsertionMode_b);

        JPanel multipleFileToInsert = new JPanel(new BorderLayout());
        multipleFileToInsert.setBorder(new TitledBorder("File"));
        JPanel multipleFileNameLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        multipleFileNameLabel.add(multipleFileName_l);
        JPanel multipleFileNameText = new JPanel(new FlowLayout(FlowLayout.LEFT));
        multipleFileNameText.add(multipleFileName_t);
        JPanel multipleFileSizeLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        multipleFileSizeLabel.add(multipleFileSize_l);
        JPanel multipleFileSizeValue = new JPanel(new FlowLayout(FlowLayout.LEFT));
        multipleFileSizeValue.add(multipleFileSize_t);
        multipleFileSizeValue.add(multipleFileSize_c);
        JPanel multipleInsertFileButton = new JPanel(new FlowLayout());
        multipleInsertFileButton.add(multipleInsertFile_b);
        JPanel multipleFileValues = new JPanel(new GridLayout(2,2));
        multipleFileValues.add(multipleFileNameLabel);
        multipleFileValues.add(multipleFileNameText);
        multipleFileValues.add(multipleFileSizeLabel);
        multipleFileValues.add(multipleFileSizeValue);

        multipleFileToInsert.add(multipleFileValues,BorderLayout.CENTER);
        multipleFileToInsert.add(multipleInsertFileButton,BorderLayout.SOUTH);

        JPanel multipleFileList = new JPanel(new BorderLayout());
        multipleFileList.setBorder(new TitledBorder(Model.translate("fileList")));

        JPanel multipleInsertionPolicyLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        multipleInsertionPolicyLabel.add(new JLabel(Model.translate("policy")));
        JPanel multipleInsertionPolicyCombo = new JPanel(new FlowLayout(FlowLayout.LEFT));
        multipleInsertionPolicyCombo.add(multipleInsertionPolicy_c);
        JPanel multipleInsertionPolicyPanel = new JPanel(new GridLayout(1,2));
        multipleInsertionPolicyPanel.add(multipleInsertionPolicyLabel);
        multipleInsertionPolicyPanel.add(multipleInsertionPolicyCombo);
        //JPanel multipleFileTableScrollPanePanel = new JPanel(new FlowLayout());

        multipleFileTableScrollPane.setPreferredSize(new Dimension(100,100));

        //multipleFileTableScrollPanePanel.add(multipleFileTableScrollPane);

       // multipleFileTableScrollPanePanel.setPreferredSize(new Dimension(100,50));
        JPanel multipleRemoveFileButton = new JPanel(new FlowLayout());
        multipleRemoveFileButton.add(multipleRemoveFile_b);

        //multipleFileTableScrollPanePanel.setBounds(0,0,100,100);


        multipleFileList.add(multipleInsertionPolicyPanel, BorderLayout.NORTH);
        multipleFileList.add(multipleFileTableScrollPane,BorderLayout.CENTER);     //// multipleFileTable
        multipleFileList.add(multipleRemoveFileButton,BorderLayout.SOUTH);
        //multipleFileList.setSize(50,50);

        //multipleFileTableScrollPane.setBounds(multipleFileTableScrollPanePanel.getBounds());

        JPanel multipleAvaibleDiskSpace = new JPanel(new FlowLayout());
        multipleAvaibleDiskSpace.setBorder(new TitledBorder(Model.translate("freeSpace")));
        multipleAvaibleDiskSpace.add(multipleAvaibleDiskSpace_t);
        multipleAvaibleDiskSpace.add(multipleAvaibleDiskSpace_l);

        multipleFileManager.setLayout(new BoxLayout(multipleFileManager,BoxLayout.Y_AXIS));
        multipleFileManager.setBorder(new TitledBorder(Model.translate("addFile")));
        multipleFileManager.add(multipleFileInsertionModeButton);
        multipleFileManager.add(multipleFileToInsert);
        multipleFileManager.add(multipleFileList);
        multipleFileManager.add(multipleAvaibleDiskSpace);

        simManager.add(hdDiskConfiguration);
        simManager.add(singleFileManager);
        simManager.add(multipleFileManager);
        //singleFileManager.setVisible(true);
        //multipleFileManager.setVisible(false);

        JPanel messages = new JPanel(new GridLayout(1,1));
        messages.setBorder(new TitledBorder(Model.translate("information")));
        messages.add(messagesText);

        resultsPanel = new SimulationResultsPanel(this);

        innerPanel = new JPanel(new CardLayout());
        innerPanel.add(simManager, CONFIG_PANEL);
        innerPanel.add(resultsPanel, RESULTS_PANEL);

        setLayout(new BorderLayout());
        add(toolBar, BorderLayout.NORTH);
        add(innerPanel, BorderLayout.CENTER);
        add(messages, BorderLayout.SOUTH);
        
        /*activePanel = CONFIG_PANEL;
        configSimulation.setEnabled(false);
        simulationGraphics.setEnabled(false);*/
        showConfig();

        readSimulationValues();

        loadSimulationFiles();
    }

    /**
     * Load the file objects contained in the simulation
     */
    public void loadSimulationFiles() {
        Simulation simulation = Model.getSimulation(simulationId);
        Vector<File> files = simulation.getFiles();
        if(files.size() > 1) {
            singleFileManager.setVisible(false);
            multipleFileManager.setVisible(true);
        }
        else {
            singleFileManager.setVisible(true);
            multipleFileManager.setVisible(false);
        }
        for(int i = 0; i < files.size(); i++) {
            String exponentString = null;
            if((files.elementAt(i)).getExponent() == 10) {
                exponentString = "KB";
            }
            if((files.elementAt(i)).getExponent() == 20) {
                exponentString = "MB";
            }
            if((files.elementAt(i)).getExponent() == 30) {
                exponentString = "GB";
            }
//            if((files.elementAt(i)).getExponent() == 40) {
//                exponentString = "TB";
//            }
            int fileDimension = (files.elementAt(i)).getDimension();
            if (exponentString != null) {
                simulationManager.setBusySpace(fileDimension, exponentString);
            }
            model.addRow(new Object[]{(files.elementAt(i)).getId(),fileDimension + " " + exponentString});
        }
    }

    /**
     * Show the simulation configuration panel
     */
    public void showConfig() {
        ((CardLayout)innerPanel.getLayout()).show(innerPanel, CONFIG_PANEL);
//        activePanel = CONFIG_PANEL;
        configSimulation.setEnabled(false);
        startSimulation.setEnabled(true);
        simulationGraphics.setEnabled(false);
        exportResults.setEnabled(false);
        resetSimulation.setEnabled(true);
    }

    /**
     * Show the simulation results panel
     */
    public void showResults() {
        ((CardLayout)innerPanel.getLayout()).show(innerPanel, RESULTS_PANEL);
        resultsPanel.fillResultsTable();
//        activePanel = RESULTS_PANEL;
        configSimulation.setEnabled(true);
        startSimulation.setEnabled(false);
        simulationGraphics.setEnabled(true);
        exportResults.setEnabled(true);
        resetSimulation.setEnabled(false);
    }

    /**
     * Return the closeSimulation instance
     * @return closeSimulation
     */
    public JButton getCloseSimulation() {
        return closeSimulation;
    }

    /**
     * Return the singleFileContiguousBlocks_t instance
     * @return singleFileContiguousBlocks_t
     */
    public JTextField getTextFragmentation() {
        return singleFileContiguousBlocks_t;//fmFragmentation_t;
    }

    /**
     * Return the selected value of singleFileDimension_c
     * @return the selected value of singleFileDimension_c
     */
    public String getComboDimFile() {
        return (singleFileDimension_c.getSelectedItem().toString());
    }

    /**
     * Return the singleFileDimension_t instance
     * @return singleFileDimension_t
     */
    public JTextField getTextDimFile() {
        return singleFileDimension_t;//fmFileDimension_t;
    }

    /**
     * Return the selected value of ext2FSAddressLevel_c
     * @return the selected value of ext2FSAddressLevel_c
     */
    public String getComboMaxAddressLevel() {
        return (ext2FSAddressLevel_c.getSelectedItem().toString());
    }

    /**
     * Return the selected value of ComboDimBlock
     * @return the selected value of ComboDimBlock
     */
    public String getComboDimBlock() {
        if (ext2FSConfiguration.isVisible()) {
            return (ext2FSBlockDimension_c.getSelectedItem().toString());
        }
        if (ntfsFSConfiguration.isVisible()) {
            return (ntfsFSBlockDimension_c.getSelectedItem().toString());
        }
        if (fat32FSConfiguration.isVisible()) {
            return (fat32FSBlockDimension_c.getSelectedItem().toString());
        }
        return null;
    }

    /**
     * Return the TextDimBlock instance
     * @return TextDimBlock
     */
    public JTextField getTextDimBlock() {
        if (ext2FSConfiguration.isVisible()) {
            return ext2FSBlockDimension_t;
        }
        if (ntfsFSConfiguration.isVisible()) {
            return ntfsFSBlockDimension_t;
        }
        if (fat32FSConfiguration.isVisible()) {
            return fat32FSBlockDimension_t;
        }
        return null;
    }

    /**
     * Return the scrollpane instance containing the results of the simulation
     * @return the scrollpane of the results
     */
    public JScrollPane getResults() {
        return resultsPanel.getSettingsScrollPane();
    }

    /*
           if (ext2FSConfiguration.isVisible()) {
            return (ext2FSBlockDimension_c.getSelectedItem().toString());
        }
        if (ntfsFSConfiguration.isVisible()) {
            return (ntfsFSBlockDimension_c.getSelectedItem().toString());
        }
        if (fat32FSConfiguration.isVisible()) {
            return (fat32FSBlockDimension_c.getSelectedItem().toString());
        }
        return null;
    }
*/
 /*           ext2FSBlockDimension_t.setText(ntfsFSBlockDimension_t.getText());
            ext2FSBlockDimension_t.setText(fat32FSBlockDimension_t.getText());
            ntfsFSBlockDimension_t.setText(ext2FSBlockDimension_t.getText());
            ntfsFSBlockDimension_t.setText(fat32FSBlockDimension_t.getText());
            fat32FSBlockDimension_t.setText(ext2FSBlockDimension_t.getText());
            fat32FSBlockDimension_t.setText(ntfsFSBlockDimension_t.getText());
        }
        if (ntfsFSConfiguration.isVisible()) {
            return ntfsFSBlockDimension_t;
        }
        if (fat32FSConfiguration.isVisible()) {
            return fat32FSBlockDimension_t;
        }
        return null;
    }
*/

    /**
     * Return the ext2FSFirstInodeDirectAddresses_t instance
     * @return ext2FSFirstInodeDirectAddresses_t
     */
    public JTextField getTextDirectBlockFirstiNode() {
        return ext2FSFirstInodeDirectAddresses_t;
    }

    /**
     * Return the ext2FSInodeDimension_t instance
     * @return ext2FSInodeDimension_t
     */
    public JTextField getTextDimiNode() {
        return ext2FSInodeDimension_t;
    }

    /**
     * Return the selected value of ComboFSType
     * @return the selected value of ComboFSType
     */
    public String getComboFSType() {
        return (ext2FSType_c.getSelectedItem().toString());
    }

    /**
     * Return the hdBlocksPerTrack_t instance
     * @return hdBlocksPerTrack_t
     */
    public JTextField getTextTrack() {
        return hdBlocksPerTrack_t;
    }

    /**
     * Return the selected value of hdDimension_c
     * @return the selected value of hdDimension_c
     */
    public String getComboDimPart() {
        return (hdDimension_c.getSelectedItem().toString());
    }

    /**
     * Return the hdDimension_t instance
     * @return hdDimension_t
     */
    public JTextField getTextDimPart() {
        return hdDimension_t;
    }

    /**
     * Return the hdFileBlocksOnTrack_t instance
     * @return hdFileBlocksOnTrack_t
     */
    public JTextField getTextBlockFileTrack() {
        return hdFileBlocksOnTrack_t;
    }

    /**
     * Return the hdRotationSpeed_t instance
     * @return hdRotationSpeed_t
     */
    public JTextField getTextRotationSpeed() {
        return hdRotationSpeed_t;
    }

    /**
     * Return the hdSeekTime_t instance
     * @return hdSeekTime_t
     */
    public JTextField getTextSeekTime() {
        return hdSeekTime_t;
    }

    /**
     * Return the resetSimulation instance
     * @return resetSimulation
     */
    public JButton getResetSimulation() {
        return resetSimulation;
    }

    /**
     * Return the SetAsDefaultSimulation instance
     * @return SetAsDefaultSimulation
     */
    public JButton getSetAsDefaultSimulation() {
        return setAsDefaultSimulation;
    }

    /**
     * Return the startSimulation instance
     * @return startSimulation
     */
    public JButton getStartSimulation() {
        return startSimulation;
    }

    /**
     * Return the id of the current simulation
     * @return the id of the simulation
     */
    public int getSimId() {
        return simulationId;
    }    

    /**
     * Activate or deactivate user interaction with the panel
     * @param active: if true it activates user interaction
     */
    public void lockWindowsDialog(boolean active) {
        if (active == true) {
            this.setEnabled(false);
            //System.out.println("Attendere attivato");
        }
        else {
            this.setEnabled(true);
            //System.out.println("Attendere disattivato Completata la gestione dei file multipli (versione non definitiva).ato");
        }
    }

    /**
     * Notify the panel with a new id for the simulation
     * @param simId: the new id of the simulation
     */
    public void notifyPanel(int simId) {
        simulationId = simId;
        lockWindowsDialog(false);
    }

    /**
     * Notify the panel with a message
     * @param message: the message to print
     */
    public void notifyPanel(String message) {
        lockWindowsDialog(false);
    }

    /**
     * Notify the panel and activate user interation
     */
    public void notifyPanel() {
        lockWindowsDialog(false);
    }

    /**
     * Reload the information of the simulation in the view
     */
    public void refreshFields() {
        simulationName = Model.getSimulationName(simulationId);

        String fsType = Model.getFsType(simulationId);
        if (fsType.equals("EXT2")) {
            ext2FSConfiguration.setVisible(true);
            ext2FSType_c.setSelectedIndex(0);
            fat32FSConfiguration.setVisible(false);
            ntfsFSConfiguration.setVisible(false);
        }
        if (fsType.equals("NTFS")) {
            ext2FSConfiguration.setVisible(false);
            fat32FSConfiguration.setVisible(false);
            fat32FSType_c.setSelectedIndex(1);
            ntfsFSConfiguration.setVisible(true);
        }
        if (fsType.equals("FAT32")) {
            ext2FSConfiguration.setVisible(false);
            fat32FSConfiguration.setVisible(true);
            ntfsFSConfiguration.setVisible(false);
            ntfsFSType_c.setSelectedIndex(2);
        }        

        if (parentPanel == null) {
            MainWidget.getTabbedPane().setTitleAt(node.getTabIndex(),
                    simulationName);
            MainWidget.getTabbedPane().updateUI();
        }

        node.setName(simulationName);
        MainWidget.updateTreeView();
    }

    /**
     * Show the errors in messagesText
     * @param error: the message to show
     */
    public void setTextError(String error) {
        messagesText.append(Model.translate(error) + "\n");
    }

    /**
     * Clear the errors text
     */
    public void clearMessages() {
        messagesText.setText("");
    }

    /**
     * Return the messagesText instance
     * @return messagesText
     */
    public JTextArea getMessagesText() {
        return messagesText;
    }

    /**
     * Sets the values of the free space
     * @param value: the value of the free space
     * @param exponent: the exponent of the free space
     */
    public void setLabelFreeDiskSpace(String value, String exponent) {
        multipleAvaibleDiskSpace_t.setText(value);
        multipleAvaibleDiskSpace_l.setText(exponent);
    }

    /**
     * Return the multipleFileName_t instance
     * @return multipleFileName_t
     */
    public JTextField getTextFileName() {
        return multipleFileName_t;
    }

    /**
     * Return the selected value of multipleFileSize_c
     * @return the selected value of multipleFileSize_c
     */
    public String getComboDimMultiFile() {
        return (multipleFileSize_c.getSelectedItem().toString());
    }

    /**
     * Return the multipleFileSize_t instance
     * @return multipleFileSize_t
     */
    public JTextField getTextDimMultiFile() {
        return multipleFileSize_t;
    }

    /**
     * Return the selected value of multipleInsertionPolicy_c
     * @return the selected value of multipleInsertionPolicy_c
     */
    public String getComboFileInsPolicy() {
        return (multipleInsertionPolicy_c.getSelectedItem().toString());
    }

    /**
     * Return the id of the current simulation
     * @return the id of the simulation
     */
    public int getSimulationId() {
        return simulationId;
    }

    /**
     * Return the name of the current simulation
     * @return the name of the simulation
     */
    public String getSimulationName() {
        return simulationName;
    }

    /**
     * Return the singleFileContiguousBlocks_t instance
     * @return singleFileContiguousBlocks_t
     */
    public JTextField getSingleFileContiguousBlocks_t() {
        return singleFileContiguousBlocks_t;
    }

    /**
     * Return the singleFileDimension_c instance
     * @return singleFileDimension_c
     */
    public JComboBox getSingleFileDimension_c() {
        return singleFileDimension_c;
    }

    /**
     * Return the singleFileDimension_t instance
     * @return singleFileDimension_t
     */
    public JTextField getSingleFileDimension_t() {
        return singleFileDimension_t;
    }

    /**
     * Enable or disable multipleInsertFile_b
     * @param enabled: if true enables the button
     */
    public void btAddFileSetEnabled(boolean enabled) {
        multipleInsertFile_b.setEnabled(enabled);
    }

    /**
     * Return the enable state of multipleInsertFile_b
     * @return true if the button is enabled
     */
    public boolean btAddFileGetEnabled() {
        return multipleInsertFile_b.isEnabled();
    }

    /**
     * Return the multipleFileName_t instance
     * @return multipleFileName_t
     */
    public JTextField getFileName() {
        return multipleFileName_t;
    }

    /**
     * Return the ntfsFSExtensionRecord_t instance
     * @return ntfsFSExtensionRecord_t
     */
    public JTextField getTextExtensionRecord() {
        return ntfsFSExtensionRecord_t;
    }

    /**
     * Return the ntfsFSMainRecord_t instance
     * @return ntfsFSMainRecord_t
     */
    public JTextField getTextMainRecord() {
        return ntfsFSMainRecord_t;
    }

    /**
     * Return the model of the inserted file table
     * @return the model of the inserted file table
     */
    public DefaultTableModel getInsertedFileTable() {
        return model;
    }

    /**
     * Sets the id of the simulation in the panel
     * @param simId: the id of the simulation in the panel
     */
    public void setId(int simId) {
        simulationId = simId;
    }

    /**
     * Return the file insertion modality
     * @return true if the modality is single file
     */
    public boolean singleInsertType(){
        if(singleFileManager.isVisible())
            return true;
        else
            return false;
    }

    /**
     * Sets the values of the max file
     * @param dimension: the size of the file
     * @param exponent: the exponent of the file
     */
    public void setLabelMaxFileDimension(String dimension, String exponent) {
        singleFileMaxDimension_t.setText(dimension);
        singleFileMaxExponent_l.setText(exponent);
    }

    /**
     * Return the height of the results table
     * @return the height of the results table
     */
    public int getTableHeight() {
        return resultsPanel.getTableHeight();
    }

    /**
     * Return the SimulationManager of this panel
     * @return the SimulationManager
     */
    public SimulationManager getSimulationManager() {
        return simulationManager;
    }

    /**
     * Return the JFrame containing the simulation graphics
     * @return the JFrame containing the simulation graphics
     */
    public JFrame getFrameGraphics() {
        return frameGraphics;
    }
}

/**
 * SimulationResultsPanel is a panel for managing simulation results
 * into a SimulationPanel
 * @author Giuseppe Biolo
 * @version %I%, %G%
 * @createData 26/05/2010
 */
class SimulationResultsPanel extends JPanel {

    private JTable settingsTable;
    private DefaultTableModel tableModel;
    private JScrollPane settingsScrollPane;
//    private JTable resultsTable;
//    private DefaultTableModel resultsModel;
//    private JScrollPane resultsScrollPane;
    private SimulationPanel parentPanel;

    /**
     * Create a SimulationResultPanel for a parent SimulationPanel
     * @param parent: the parent SimulationPanel
     */
    public SimulationResultsPanel(SimulationPanel parent) {
        parentPanel = parent;

        initComponents();

        //setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setLayout(new FlowLayout(FlowLayout.LEFT));
        add(settingsScrollPane);
        //add(resultsScrollPane);
    }

    /**
     * Initialize class fields and settings
     */
    private void initComponents() {
        settingsTable = new JTable();
        settingsTable.setRowHeight(20);
        settingsTable.setEnabled(false);
        settingsScrollPane = new JScrollPane(settingsTable);
        //settingsScrollPane.setBorder(new TitledBorder("Valori impostati"));

//        resultsTable = new JTable();
//        resultsTable.setRowHeight(20);
//        resultsTable.setEnabled(false);
//        resultsScrollPane = new JScrollPane(resultsTable);
//        resultsScrollPane.setBorder(new TitledBorder(Model.translate("simulationResult")));

        String[] columnNames = {
            Model.translate("parameters"),
            Model.translate("value")
        };

        tableModel = new DefaultTableModel(columnNames, 0);
        settingsTable.setModel(tableModel);
//        resultsModel = new DefaultTableModel(columnNames, 0);
//        resultsTable.setModel(resultsModel);
    }

    /**
     * Return the scroll pane containing the results of the simulation
     * @return the scroll pane containing the results of the simulation
     */
    public JScrollPane getSettingsScrollPane() {
        return settingsScrollPane;
    }

    /**
     * Return the height of the table containing the results of the simulation
     * @return the height of the table containing the results of the simulation
     */
    public int getTableHeight() {
        return settingsTable.getHeight() + 17;
    }

    /**
     * Fill the results table with the settings and results of the simulation
     */
    public void fillResultsTable() {
        int simId = parentPanel.getSimulationId();

        // clearing table
        tableModel.setRowCount(0);

        Vector<String[]> config = Model.getSimulation(simId).getConfigurationValues();
        Vector<String[]> results = Model.getSimulation(simId).getResultsValues();

        for (int i=0; i<config.size(); i++) {
            tableModel.addRow(config.elementAt(i));
        }
        for (int i=0; i<results.size(); i++){
            tableModel.addRow(results.elementAt(i));
        }
    }
}
